<?php

namespace App\Entity\Lang;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use App\Repository\Lang\LangPackRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=LangPackRepository::class)
 */
class LangPack
{
    /**
     * @Common(cnName="语言名称",listType="text",editType="text")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"lang"})
     */
    private $id;

    /**
     * @Common(cnName="语言",listType="text",editType="text")
     * @ORM\ManyToOne(targetEntity=Language::class, inversedBy="packs")
     * @ORM\JoinColumn(nullable=false)
     * @Groups({"lang"})
     */
    private $language;

    /**
     * @Common(cnName="页面",listType="text",editType="text")
     * @ORM\OneToMany(targetEntity=LangPage::class, mappedBy="pack", orphanRemoval=true)
     * @Groups({"lang"})
     */
    private $pages;

    /**
     * @Common(cnName="语言包名称",listType="text",editType="text")
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Groups({"lang"})
     */
    private $name;

    public function __construct()
    {
        $this->pages = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getLanguage(): ?Language
    {
        return $this->language;
    }

    public function setLanguage(?Language $language): self
    {
        $this->language = $language;

        return $this;
    }

    /**
     * @return Collection|LangPage[]
     */
    public function getPages(): Collection
    {
        return $this->pages;
    }

    public function addPage(LangPage $page): self
    {
        if (!$this->pages->contains($page)) {
            $this->pages[] = $page;
            $page->setPack($this);
        }

        return $this;
    }

    public function removePage(LangPage $page): self
    {
        if ($this->pages->contains($page)) {
            $this->pages->removeElement($page);
            // set the owning side to null (unless already changed)
            if ($page->getPack() === $this) {
                $page->setPack(null);
            }
        }

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }
}
